/*
/**
 * This file is part of jCGT (the Java Cooperative Game Theory library).
 *
 * jCGT is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jCGT is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with jCGT.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package cooperativegametheory.gui;

import cooperativegametheory.solutionfunctions.AumanDrezeValue;
import cooperativegametheory.solutionfunctions.CasajusValue;
import cooperativegametheory.Partition;
import cooperativegametheory.solutionfunctions.SolutionFunction;
import cooperativegametheory.solutionfunctions.WieseValue;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.ListSelectionModel;

/**
 *
 * @author Jonas Brekle <jonas.brekle@gmail.com>
 */
public class SFSettingsPartition extends SFSettingsPanel {

    CFSettingsPanel cfSettings;
    ReportingListTransferHandler arrayListHandler = new ReportingListTransferHandler();
    DefaultListModel model;
    final static String sep = "----";
    String sfName;

    /** Creates new form SFSettingsPartition */
    public SFSettingsPartition(CFSettingsPanel cfS, String sfType) {
        initComponents();
        cfSettings = cfS;
        sfName = sfType;
        model = new DefaultListModel();
        for (int i = 0; i < cfSettings.getCF().getNumPlayers(); i++) {
            model.addElement(String.valueOf(i+1));
        }
        playerList.setModel(model);
        playerList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        playerList.setTransferHandler(arrayListHandler);
        playerList.setDragEnabled(true);
    }

    protected int[][] getPartition(){
        ArrayList<ArrayList<Integer>> pAL = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> curr = new ArrayList<Integer>();
        pAL.add(curr);
        for(int i = 0; i < model.size(); i++){
            if(((String) model.elementAt(i)).contentEquals(sep)){
                curr = new ArrayList<Integer>();
                pAL.add(curr);
            } else {
                curr.add(Integer.valueOf((String) model.elementAt(i)));
            }
        }

        int[][] p = new int[pAL.size()][];
        for(int i = 0; i < pAL.size(); i++){
            p[i] = new int[pAL.get(i).size()];
            for(int j = 0; j < pAL.get(i).size(); j++){
                p[i][j] = pAL.get(i).get(j) -1;
            }
        }

        return p;
    }

    @Override
    public SolutionFunction getSF() {
        try {
            if(sfName.contentEquals("wiese")){
                return new WieseValue(cfSettings.getCF(), new Partition(getPartition(),cfSettings.getCF().getPlayers()));
            } else if(sfName.contentEquals("ad")){
                return new AumanDrezeValue(cfSettings.getCF(), new Partition(getPartition(),cfSettings.getCF().getPlayers()));
            } else if(sfName.contentEquals("casajus")){
                return new CasajusValue(cfSettings.getCF(), new Partition(getPartition(),cfSettings.getCF().getPlayers()));
            }
        } catch (ExceptionInInitializerError e){
            
        }
        return null;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        playerList = new javax.swing.JList();
        addSeparatorButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        playerList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        playerList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        playerList.setDragEnabled(true);
        jScrollPane1.setViewportView(playerList);

        jScrollPane2.setViewportView(jScrollPane1);

        addSeparatorButton.setText("add Separator");
        addSeparatorButton.setActionCommand("addSeparatorClick");
        addSeparatorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addSeparatorButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Partition");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addSeparatorButton)))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(55, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addSeparatorButton))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addSeparatorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addSeparatorButtonActionPerformed
        model.addElement(sep);
    }//GEN-LAST:event_addSeparatorButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addSeparatorButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList playerList;
    // End of variables declaration//GEN-END:variables

    @Override
    void setSubType(String sT) {
        sfName = sT;
    }
}
